<template>
  <el-dialog title="分配角色" :visible="showRoleDialog">
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
       <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById , assignRole } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
      require: true,
    }
  },
  data() {
    return {
      list: [],
      roleIds: [],
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { rows } = await getRoleList({page:1, pagesize: 6})
      this.list = rows;
    },
     async getUserDetailById(id) {
      // props数据传递是异步的
      const { roleIds } = await getUserDetailById(id);
      this.roleIds = roleIds;
    },
    // 确定
  async btnOk () {
    await assignRole({id: this.userId, roleIds: this.roleIds});
    this.$emit("update:showRoleDialog", false);
  },
  cancel () {
    this.roleIds = [];
    this.$emit("update:showRoleDialog", false)
  }
  },  
  
};
</script>